package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.SettingsPersonalBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.view.ClearableEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/5/4.
 * 个人资料修改详细地址页面
 */

public class MyAddressActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.cet_address)
    ClearableEditText cetAddress;
    @BindView(R.id.rl_baocun)
    RelativeLayout rlBaocun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address);
        ButterKnife.bind(this);
        setView();
    }

    private void setView() {
        cetAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
    }
    private void editTextIsNo() {
        if (TextUtils.isEmpty(cetAddress.getText())/*||TextUtils.isEmpty(etShenbaozhuti.getText())*/){
            rlBaocun.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(cetAddress.getText())/*&&!TextUtils.isEmpty(etShenbaozhuti.getText())*/){
            rlBaocun.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }
    @OnClick({R.id.iv_back, R.id.rl_baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_baocun:
                if (TextUtils.isEmpty(cetAddress.getText())/*||TextUtils.isEmpty(cetMima.getText())*/){
                    return;
                }
                if (!TextUtils.isEmpty(cetAddress.getText())/*&&!TextUtils.isEmpty(cetMima.getText())*/){
                    spUtils.put(AppConstant.ADDRESS,cetAddress.getText().toString());
                    settingCenter() ;
                }

                break;
        }
    }

    private void settingCenter() {
        Log.i("5555", "settingCenter: "+spUtils.getString(AppConstant.PROVINCE)+spUtils.getString(AppConstant.CITY)+spUtils.getString(AppConstant.COUNTY));
        HttpMethods.getInstanceCenter().info.getSettingCenter(
                spUtils.getString(AppConstant.USER_NAME)
                ,spUtils.getString(AppConstant.TRUE_NAME)
                ,spUtils.getString(AppConstant.ADDRESS)
                ,spUtils.getString(AppConstant.EMAIL)
                ,spUtils.getString(AppConstant.NICKNAME)
                ,spUtils.getString(AppConstant.MOBILE)
                ,spUtils.getString(AppConstant.CREDITID)
                ,spUtils.getString(AppConstant.PROVINCE)
                ,spUtils.getString(AppConstant.CITY)
                ,spUtils.getString(AppConstant.COUNTY)
        )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingsPersonalBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingsPersonalBean settingsPersonalBean) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        EventBus.getDefault().post("address");
                        finish();

                    }
                });

    }
}
